/*
 * File: GetMessages.java Author: Robert Bittle <guywithnose@gmail.com>
 */
package servlets.admin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;

import faceBook.Message;

import java.util.ArrayList;
import java.util.GregorianCalendar;

/**
 * The Class GetMessages.
 */
public class GetMessagesByDate extends HttpServlet
{

  /*
   * (non-Javadoc)
   * 
   * @see
   * javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
   * javax.servlet.http.HttpServletResponse)
   */
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException
  {
    String dateString = req.getParameter("date");
    String[] dateParts = dateString.split("/");
    int month = Integer.valueOf(dateParts[0])-1;
    int day = Integer.valueOf(dateParts[1]);
    int year = Integer.valueOf(dateParts[2]);
    GregorianCalendar date = new GregorianCalendar(year, month, day);
    ArrayList<Message> messages = Message.getAllMessageForDay(date);
    JSONArray ja = new JSONArray();
    for (Message m : messages)
    {
      ja.put(m.toJSONObject());
    }
    resp.getWriter().write(ja.toString());
  }

}
